{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xml.etree.ElementTree as ET\n",
    "from os import getcwd\n",
    "import os\n",
    "from tqdm.notebook import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_train = 'train\\\\'\n",
    "dataset_file = '4_CLASS_test.txt'\n",
    "classes_file = dataset_file[:-4]+'_classes.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "CLS = os.listdir(dataset_train)\n",
    "classes =[dataset_train+CLASS for CLASS in CLS]\n",
    "wd = getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test(fullname):\n",
    "    bb = \"\"\n",
    "    in_file = open(fullname)\n",
    "    tree=ET.parse(in_file)\n",
    "    root = tree.getroot()\n",
    "    for i, obj in enumerate(root.iter('object')):\n",
    "        difficult = obj.find('difficult').text\n",
    "        cls = obj.find('name').text\n",
    "        if cls not in CLS or int(difficult)==1:\n",
    "            continue\n",
    "        cls_id = CLS.index(cls)\n",
    "        xmlbox = obj.find('bndbox')\n",
    "        b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))\n",
    "        bb += (\" \" + \",\".join([str(a) for a in b]) + ',' + str(cls_id))\n",
    "    #\n",
    "    #    # we need this because I don't know overlapping or something like that\n",
    "    #    if cls == 'Traffic_light':\n",
    "    #        list_file = open(dataset_file, 'a')\n",
    "    #        file_string = str(fullname)[:-4]+'.jpg'+bb+'\\n'\n",
    "    #        list_file.write(file_string)\n",
    "    #        list_file.close()\n",
    "    #        bb = \"\"\n",
    "    #\n",
    "    if bb != \"\":\n",
    "        list_file = open(dataset_file, 'a')\n",
    "        file_string = str(fullname)[:-4]+'.png'+bb+'\\n'\n",
    "        list_file.write(file_string)\n",
    "        list_file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0fbf56b18f974c579d0be4c53aae332a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=1.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "for CLASS in tqdm(classes):\n",
    "    for filename in os.listdir(CLASS):\n",
    "        if not filename.endswith('.xml'):\n",
    "            continue\n",
    "        fullname = os.getcwd()+'\\\\'+CLASS+'\\\\'+filename\n",
    "        test(fullname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7145ee90d59a494185b0c082458df1ce",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=1.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "for CLASS in tqdm(CLS):\n",
    "    list_file = open(classes_file, 'a')\n",
    "    file_string = str(CLASS)+\"\\n\"\n",
    "    list_file.write(file_string)\n",
    "    list_file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'seal\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "file_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
